package com.mby.service.impl;

import com.mby.entity.*;
import com.mby.service.IPushService;
import com.mby.service.IRongYunService;
import com.mby.utils.*;
import io.rong.models.message.GroupMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static com.mby.controller.BaseController.rongCloud;

/**
 * Created by developer_01 on 2018/6/11.
 */
@Service("rongYunService")
public class RongYunService implements IRongYunService {

    @Resource
    public MongoTools mongoTool;

    public void sentStatusMsg(ChatGroup cg,Integer status) throws Exception{
        User user  = mongoTool.findOne("","{'id':"+cg.getCreatedBy()+"}",User.class);
        String portrait;
        RongUser rongUser = new RongUser();
        if(WebConstant.APP_ENV.equals("prod")){
            portrait = "http://live-asset.themobiyun.com/userIcon/"+user.getId()+"?"+new Date().getTime();
        }else{
            portrait = "http://live-asset.themobiyun.com/userIcon/dev/"+user.getId()+"?"+new Date().getTime();
        }
        rongUser.setUserId(user.getId());
        rongUser.setId(user.getId());
        rongUser.setName(user.getUserName()+"（"+user.getRoleName().split(",")[0]+"）");
        rongUser.setPlace(user.getRoleName());
        rongUser.setPortrait(portrait);
        rongUser.setUser(rongUser);
        StatusMessage message = new StatusMessage(WebConstant.CS_VEHICLE_STATUS_MSG_CONTENT, status, "status", rongUser);
        GroupMessage groupMessage = new GroupMessage()
                .setSenderId(cg.getCreatedBy().toString())
                .setTargetId(cg.getId().toString().split(","))
                .setObjectName("MBY:StatusMsg")
                .setContent(message)
                .setIsIncludeSender(1);
        rongCloud.message.group.send(groupMessage);
    }
}
